﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_Categories : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    CategoriesBLL c = new CategoriesBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();
            rbtnStatus.Enabled = false;
            btnUpdate.Enabled = false;
        }
    }
    private void loadData()
    {
        gvCategory.DataSource = c.getCategories();
        gvCategory.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }

        category cat = new category
        {
            categoryname = txtCatName.Text,
            descriptions = txtDesc.Text,
            pagesite = txtPagesite.Text,
            status=status
        };
        c.Category_Insert(cat);
        this.loadData();
        this.reset();
    }
    public void reset()
    {
        txtCatID.Text = String.Empty;
        txtCatName.Text = String.Empty;
        txtDesc.Text = String.Empty;
        txtPagesite.Text = String.Empty;
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void gvCategory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txtCatID.Text = Server.HtmlDecode(gvCategory.Rows[e.NewSelectedIndex].Cells[0].Text);
        txtCatName.Text = Server.HtmlDecode(gvCategory.Rows[e.NewSelectedIndex].Cells[1].Text);
        txtDesc.Text = Server.HtmlDecode(gvCategory.Rows[e.NewSelectedIndex].Cells[2].Text);
        txtPagesite.Text = Server.HtmlDecode(gvCategory.Rows[e.NewSelectedIndex].Cells[3].Text);
        
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        category cat = new category
        {
            id = int.Parse(txtCatID.Text),
            categoryname = txtCatName.Text,
            descriptions = txtDesc.Text,
            pagesite = txtPagesite.Text,
            status = status
        };
        c.Category_Update(cat);
        this.loadData();
        this.reset();
    }
    protected void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string CatID = gvCategory.Rows[e.RowIndex].Cells[0].Text;
        c.Category_Delete(int.Parse(CatID));
        this.loadData();
    }
    protected void gvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCategory.PageIndex = e.NewPageIndex;
        this.loadData();
    }
}